package thread;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;


public class threadLogomarca extends JFrame implements KeyListener{
	private static final long serialVersionUID = 1L;
	
	private int sentido;
	private JLabel lLogo;
	private Thread moveLogo;
	private JMenuBar jmenuBar;
	private JMenu testMenu;
	private JMenuItem Item1, Item2, Item3, Item4;
	
	public threadLogomarca() {
		this.sentido=1;
		this.lLogo=new JLabel(new ImageIcon(getClass().getResource("logo.png")));
		
		this.jmenuBar=new JMenuBar();
		add(jmenuBar).setBounds(0, 0, 600, 30);
		
		this.testMenu=new JMenu("inicio");
		this.jmenuBar.add(testMenu);
		
		this.Item1=new JMenuItem("teste1");
		this.Item1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
				
			}
		});
		testMenu.add(Item1);
		
		this.Item2=new JMenuItem("teste2");
		this.Item2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(null, "Okay, Baby");
				
			}
		});
		testMenu.add(Item2);
		
		this.Item3=new JMenuItem("teste3");
		testMenu.add(Item3);
		
		this.Item4=new JMenuItem("teste4");
		testMenu.add(Item4);
		
		this.lLogo.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent arg0) {}
			public void mousePressed(MouseEvent arg0) {
			}
			public void mouseExited(MouseEvent arg0) {}
			public void mouseEntered(MouseEvent arg0) {}
			public void mouseClicked(MouseEvent arg0) {}
		});
		add(lLogo).setBounds(10, 10, 60, 60);
		
		this.moveLogo=new Thread(new Runnable() {
			public void run() {
				while (true) {
					switch (sentido) {
					case 1:
						lLogo.setLocation(lLogo.getX()+1, lLogo.getY());
						
						if (lLogo.getX()==530 && lLogo.getY()==10) sentido=2;
						
						else if (lLogo.getX()==530 && lLogo.getY()==530) sentido=3;
						
						else if (lLogo.getX()==530 && lLogo.getY()==270) sentido=4;
						
						break;
						
					case 2:
						lLogo.setLocation(lLogo.getX(), lLogo.getY()+1);
						
						if (lLogo.getX()==530 && lLogo.getY()==270) sentido=3;
						
						else if (lLogo.getX()==10 && lLogo.getY()==530) sentido=1;
						
						break;
						
					case 3:
						lLogo.setLocation(lLogo.getX()-1, lLogo.getY());
						
						if (lLogo.getX()==10 && lLogo.getY()==270) sentido=2;
						
						else if (lLogo.getX()==10 && lLogo.getY()==530) sentido=4;
						
						else if (lLogo.getX()==10 && lLogo.getY()==10) sentido=1;
						
						break;
						
					case 4:
						lLogo.setLocation(lLogo.getX(), lLogo.getY()-1);
						
						if (lLogo.getX()==10 && lLogo.getY()==270) sentido=1;
						
						else if (lLogo.getX()==530 && lLogo.getY()==10) sentido=3;
						
						break;
						
					}
					
					try {
						Thread.sleep(5);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
				}
				
			}
		});
		
		moveLogo.start();
		
		addKeyListener(this);
		
		setSize(600, 600);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setUndecorated(true);
		setLocationRelativeTo(null);
		setLayout(null);
		setVisible(true);
		
	}
	

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
			System.exit(0);
		}
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public static void main(String[] args) {
		new threadLogomarca();
	}

}